package ce.com.cenewbluesdk.entity.k3;

import android.os.Parcel;
import android.os.Parcelable;



import java.util.TimeZone;

import ce.com.cenewbluesdk.CEBC;
import ce.com.cenewbluesdk.entity.CEDevData;
import ce.com.cenewbluesdk.uitl.ByteUtil;
import ce.com.cenewbluesdk.uitl.TimeUtil;

/**
 * Created by Administrator on 2015/7/2.
 */
public class Send_Time_Sync_Struct_K3 implements Parcelable {

    byte[] abs_time = new byte[4]; //绝对时间 s
    byte[] offset_time = new byte[4]; //时区偏移时间 s

    public Send_Time_Sync_Struct_K3(long absLong, long offsetLong) {
        abs_time = ByteUtil.intToByte((int) (absLong / 1000));
        offset_time = ByteUtil.intToByte4((int) (offsetLong / 1000));
    }

    protected Send_Time_Sync_Struct_K3(Parcel in) {
        abs_time = in.createByteArray();
        offset_time = in.createByteArray();
    }

    public static final Creator<Send_Time_Sync_Struct_K3> CREATOR = new Creator<Send_Time_Sync_Struct_K3>() {
        @Override
        public Send_Time_Sync_Struct_K3 createFromParcel(Parcel in) {
            return new Send_Time_Sync_Struct_K3(in);
        }

        @Override
        public Send_Time_Sync_Struct_K3[] newArray(int size) {
            return new Send_Time_Sync_Struct_K3[size];
        }
    };

    public static Send_Time_Sync_Struct_K3 getCurrentTime_K3() {
        long now = TimeUtil.now();
        return new Send_Time_Sync_Struct_K3(now, TimeZone.getDefault().getOffset(now));
    }

    public byte[] getBytes() {
        byte[] bytes = new byte[abs_time.length + offset_time.length];
        System.arraycopy(abs_time, 0, bytes, 0, offset_time.length);
        System.arraycopy(offset_time, 0, bytes, 4, 4);
        return bytes;
    }

    public static int getItemSize() {
        return 8;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeByteArray(abs_time);
        dest.writeByteArray(offset_time);
    }


    public CEDevData toCEDevData() {
        CEDevData data=new CEDevData();
        data.setCmd(CEBC.CMD_SEND_TYPE);
        data.setDataType(CEBC.K3.DATA_TYPE_TIME);
        data.setData(getBytes());
        data.setItemL(8);
        data.setItemNumber(1);
        return data;
    }
}
